﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.Mvc;

using ProjectBase.Core;

using Survey.Common.Business.Services;
using Survey.Common.Data.Services;
using Survey.Entity.Services;

namespace Survey.Business.Services
{
    public class TranslatorFacade
        : BaseFacade<Translator, int, TranslatorFilter, ITranslatorDao>, ITranslatorFacade
    {
        #region IBaseFacade<T> override - FACTORY METHOD
        public override Translator CreateNew()
        {
            var entity = new Translator
            {
                Key = string.Empty,
                Area = string.Empty
            };
            return entity;
        }
        #endregion IBaseFacade<T> override - FACTORY METHOD

        #region Bind
        public override bool BindEntity(Translator entity, IValueProvider valueProvider, string prefix = null, IEnumerable<string> includeProperties = null, IEnumerable<string> excludeProperties = null)
        {
            return base.BindEntity(entity, valueProvider, prefix, includeProperties, excludeProperties)
                && BindPhrases(entity, valueProvider);
        }
        public override bool BindFilter(TranslatorFilter filter, IValueProvider valueProvider, string prefix = null, IEnumerable<string> includeProperties = null, IEnumerable<string> excludeProperties = null)
        {
            if (valueProvider.ContainsPrefix("SelectedLanguage"))
            {
                filter.SearchedLanguageName = valueProvider.GetValue("SelectedLanguage").AttemptedValue;
            }
            return base.BindFilter(filter, valueProvider, prefix, includeProperties, excludeProperties);
        }

        protected virtual bool BindPhrases(Translator entity, IValueProvider valueProvider)
        {
            foreach (var lang in LanguageFacade.GetAll())
            {
                //if (Request.Form.AllKeys.Contains(lang.LanguageName))
                //{
                //    Model.Item.Translations[lang.LanguageName] = Request.Form[lang.LanguageName];
                //}
                if (valueProvider.ContainsPrefix(lang.LanguageName))
                {
                    entity.Translations[lang.LanguageName] = valueProvider.GetValue(lang.LanguageName).AttemptedValue;
                }
            }
            return true;
        }
        #endregion Bind

        #region constructor
        static TranslatorFacade()
        {
            ChangeHandler += Clear;
        }
        #endregion constructor

        #region ITranslatorFacade
        protected static IEnumerable<Translator> Translators { get; set; }

        public virtual IEnumerable<Translator> GetAll()
        {
            CheckFileDependency();

            var translators = Translators;
            if (translators.IsNull())
            {
                translators = Dao.GetAll().Select(e => e.Clone() as Translator).ToList().AsReadOnly();
                Translators = translators;
            }
            return translators;
        }

        static void Clear(object sender, EventArgs e)
        {
            Translators = null;
        }
        #endregion ITranslatorFacade

        #region properties
        public virtual ILanguageFacade LanguageFacade { protected get; set; }
        #endregion properties
    }
}